<?php
/**
 * 主动请求企业微信接口封装
 * Created by PhpStorm.
 * User: sh01
 * Date: 2018/1/9
 * Time: 9:26
 */

namespace corpWeixin;
use corpWeixin\corpWeixinSdk\CWXTool;
use corpWeixin\corpWeixinSdk\Curl;

class CWXUser
{
    public function findUsersByDepartmentId($id,$fetchChild=0)
    {
        /* @var $cwxTool \corpWeixin\corpWeixinSdk\CWXTool*/
        $cwxTool = CWXTool::getInstance();
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token='.$cwxTool->getAccessToken()
            .'&department_id='.$id.'&fetch_child='.$fetchChild;
        $res = json_decode(Curl::httpGet($url),true);
        if(isset($res['errcode']) && $res['errcode']===0)
            return $res['userlist'];
        return false;
    }

    public function findUserByCode($code)
    {
        /* @var $cwxTool \corpWeixin\corpWeixinSdk\CWXTool*/
        $cwxTool = CWXTool::getInstance();
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$cwxTool->getAccessToken().'&code='.$code;
        $res = json_decode(Curl::httpGet($url),true);
        if(isset($res['errcode']) && $res['errcode']===0 && isset($res['user_ticket']))
        {
            return $res;
        }
        return false;
    }

    public function findUserDetailByUserTicket($userTicket)
    {
        /* @var $cwxTool \corpWeixin\corpWeixinSdk\CWXTool*/
        $cwxTool = CWXTool::getInstance();
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token='.$cwxTool->getAccessToken();
        $data = '{"user_ticket":"'.$userTicket.'"}';
        $res = json_decode(Curl::httpPost($url,$data),true);
        if(isset($res['errcode']) && $res['errcode']===0)
            return $res;
        return false;

    }
}